package com.ibm.csorb.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ibm.csorb.dao.CsCycleDao;
import com.ibm.csorb.entity.CsCycle;

@Component
@Transactional
public class CsCycleDaoImpl extends HibernateDaoSupport implements CsCycleDao {
	private static final Log log = LogFactory.getLog(CsCycleDaoImpl.class);
	// property constants
	public static final String CYCLE_NAME = "cycleName";
	public static final String REQUESTOR = "requestor";
	public static final String JCC_VERSION = "jccVersion";
	public static final String JCC_LOCK = "jccLock";
	public static final String RUN_TYPE = "runType";
	public static final String DB2_VERSION = "db2Version";
	public static final String DB2_LEVEL = "db2Level";
	public static final String DB2_TYPE = "db2Type";
	public static final String JAVA_VERSION = "javaVersion";
	public static final String DB2_SERVER = "db2Server";
	public static final String TEST_CLIENT = "testClient";
	public static final String IS_IPL = "isIpl";
	public static final String IS_TCPUN = "isTcpun";
	public static final String IS_BINDDRIVER = "isBinddriver";
	public static final String IS_LOAD = "isLoad";

	protected void initDao() {
		// do nothing
	}

	public void save(CsCycle transientInstance) {
		log.debug("saving CsCycle instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CsCycle persistentInstance) {
		log.debug("deleting CsCycle instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CsCycle findById(java.lang.Integer id) {
		log.debug("getting CsCycle instance with id: " + id);
		try {
			CsCycle instance = (CsCycle) getHibernateTemplate().get(
					"com.ibm.csorb.entity.CsCycle", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<CsCycle> findByExample(CsCycle instance) {
		log.debug("finding CsCycle instance by example");
		try {
			List<CsCycle> results = getHibernateTemplate().findByExample(
					instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<CsCycle> findByProperty(String propertyName, Object value) {
		log.debug("finding CsCycle instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CsCycle as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<CsCycle> findByCycleName(Object cycleName) {
		return findByProperty(CYCLE_NAME, cycleName);
	}

	public List<CsCycle> findByRequestor(Object requestor) {
		return findByProperty(REQUESTOR, requestor);
	}

	public List<CsCycle> findByJccVersion(Object jccVersion) {
		return findByProperty(JCC_VERSION, jccVersion);
	}

	public List<CsCycle> findByJccLock(Object jccLock) {
		return findByProperty(JCC_LOCK, jccLock);
	}

	public List<CsCycle> findByRunType(Object runType) {
		return findByProperty(RUN_TYPE, runType);
	}

	public List<CsCycle> findByDb2Version(Object db2Version) {
		return findByProperty(DB2_VERSION, db2Version);
	}

	public List<CsCycle> findByDb2Level(Object db2Level) {
		return findByProperty(DB2_LEVEL, db2Level);
	}

	public List<CsCycle> findByDb2Type(Object db2Type) {
		return findByProperty(DB2_TYPE, db2Type);
	}

	public List<CsCycle> findByJavaVersion(Object javaVersion) {
		return findByProperty(JAVA_VERSION, javaVersion);
	}

	public List<CsCycle> findByDb2Server(Object db2Server) {
		return findByProperty(DB2_SERVER, db2Server);
	}

	public List<CsCycle> findByTestClient(Object testClient) {
		return findByProperty(TEST_CLIENT, testClient);
	}

	public List<CsCycle> findByIsIpl(Object isIpl) {
		return findByProperty(IS_IPL, isIpl);
	}

	public List<CsCycle> findByIsTcpun(Object isTcpun) {
		return findByProperty(IS_TCPUN, isTcpun);
	}

	public List<CsCycle> findByIsBinddriver(Object isBinddriver) {
		return findByProperty(IS_BINDDRIVER, isBinddriver);
	}

	public List<CsCycle> findByIsLoad(Object isLoad) {
		return findByProperty(IS_LOAD, isLoad);
	}

	@SuppressWarnings("unchecked")
	public List<CsCycle> findAll() {
		log.debug("finding all CsCycle instances");
		try {
			String queryString = "from CsCycle";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CsCycle merge(CsCycle detachedInstance) {
		log.debug("merging CsCycle instance");
		try {
			CsCycle result = (CsCycle) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CsCycle instance) {
		log.debug("attaching dirty CsCycle instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CsCycle instance) {
		log.debug("attaching clean CsCycle instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CsCycleDaoImpl getFromApplicationContext(
			ApplicationContext ctx) {
		return (CsCycleDaoImpl) ctx.getBean("CsCycleDAO");
	}
}